package com.aia.prm.web.sanity;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.aia.prm.domain.VOFile;
import com.aia.prm.service.DefaultService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class NewimageAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NewimageAction.class);

	private static final long serialVersionUID = 1L;
	private DefaultService defaultService;
	private File image;
	private String imageFileName;
	private String fid;
	private String tag;

	public void validate() {
		Map session = ActionContext.getContext().getSession();

		if (session.get("PrmApplication_user") == null) {
			addActionError("您未登录后台或者会话超时");
			return;
		}

		if (Tools.isEmpty(fid)) {
			addFieldError("fid", "请输入id");
			return;
		}

		if (imageFileName == null || imageFileName.length() == 0) {
			addFieldError("image", "图片文件过大或未选择图片");
			return;
		}
		if (imageFileName.toLowerCase().endsWith(".jpg") || imageFileName.toLowerCase().endsWith(".png")) {
		} else {
			addFieldError("image", "请选择jpg或png格式的图片");
			return;
		}
		if (image == null || image.length() == 0) {
			addFieldError("image", "选择了空文件");
			return;
		}
		if (image.length() > 1024 * 1024) {
			addFieldError("image", "您选择的文件大小超过了1024KB");
			return;
		}
	}

	/***
	 * nouse
	 */
	public String execute() {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		logger.debug("得到文件名:" + imageFileName);
		logger.debug("原文件大小:" + image.length() + "字节");
		logger.debug("上传至服务器的临时文件为: \r\n <br> " + image.getAbsolutePath());

		byte[] thumbBytes = null;
		try {
			thumbBytes = FileUtils.readFileToByteArray(image);
		} catch (IOException e) {
			addActionError("出错了！" + e);
			logger.error("error", e);
			return INPUT;
		}

		VOFile myImage = new VOFile();
		myImage.setF_ID(fid);
		myImage.setF_TAG(tag);
		myImage.setF_DATE(Tools.getToday());
		myImage.setF_TIME(Tools.getTime());
		myImage.setF_BIN(thumbBytes);
		try {
			defaultService.saveFile(myImage);
		} catch (Exception e) {
			addActionError("出错了，可能是主键冲突，请查看详细日志");
			logger.error("e", e);
			return INPUT;
		}

		addActionMessage("保存成功 <a target='_blank' href='../dbImage.jsp?id=" + fid + "' >点击此链接查看图片</a>");
		addActionMessage("您可以继续上传图片：");

		setFid("");
		setTag("");
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public File getImage() {
		return image;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public String getFid() {
		return fid;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

	public DefaultService getDefaultService() {
		return defaultService;
	}

	public void setDefaultService(DefaultService defaultService) {
		this.defaultService = defaultService;
	}

	public String getTag() {
		return tag;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

}
